package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JLabel;
import javax.swing.JPanel;
import model.Environment;
import model.turtle.Commandable;

/**
 * InformationScreen.java shows the information about
 *  the Commandable object used in SLogo.
 *
 * @author Matthew Roy and Angelica Schwartz
 */
public class InformationScreen extends JPanel implements Observer {

    /**
     * default serialVersionUID.
     */
    private static final long serialVersionUID = 1L;
    /**
     * InfoScreen displays the information for this
     *  Environment object.
     */
    private Environment myEnvironment;
    /**
     * used to display the title.
     */
    private JLabel myTitleLabel;
    /**
     * used to display the x-coordinate of
     *      the Commandable object.
     */
    private JLabel myXCoordLabel;
    /**
     * used to display the y-coordinate of
     *      the Commandable object.
     */
    private JLabel myYCoordLabel;
    /**
     * used to display the facing direction of
     *      the Commandable object.
     */
    private JLabel myDirectionLabel;

    /**
     * constructor.
     *
     * @param size is default size of the screen
     * @param e is the Environment object
     */
    public InformationScreen(Dimension size, Environment e) {
        // set size of screen
        setPreferredSize(size);
        setSize(size);
        createLabels();
        myEnvironment = e;
        myEnvironment.addObserver(this);
    }

    /**
     * Creates initial labels.
     */
    private void createLabels() {
        //default values
        myTitleLabel = new JLabel("Workspace Information     ");
        myXCoordLabel = new JLabel("Current XCOORD: 0");
        myYCoordLabel = new JLabel("Current YCOORD: 0");
        myDirectionLabel = new JLabel("Current Direction: 0");
        add(myTitleLabel);
        add(myXCoordLabel);
        add(myYCoordLabel);
        add(myDirectionLabel);
    }

    /**
     * Paint the contents of the canvas.
     *
     * Never called by us directly, instead called by Java runtime
     * when area of screen covered by this container needs to be
     * displayed (i.e., creation, uncovering, change in status)
     *
     * @param pen used to paint shape on the screen
     */
    @Override
    public void paintComponent(Graphics pen) {
        pen.setColor(Color.LIGHT_GRAY);
        pen.fillRect(0, 0, getSize().width, getSize().height);
        setCommandableInformation();
    }


    /**
     * Helper method to draw the information about
     *   the Commandable object.
     */
    private void setCommandableInformation() {
        Commandable c = myEnvironment.getFocusedCommandable();
        myXCoordLabel.setText("Current XCOORD: "
                + (int)c.getXCoordinate() + "   ");
        myYCoordLabel.setText("Current YCOORD: "
                + (int)c.getYCoordinate() + "   ");
        myDirectionLabel.setText("Current Direction: "
                +  (int)c.getHeading());
    }


    /**
     * implemented Observer interface method. Called when
     *  notifyObservers() is called when there is a
     *  change in the model.
     *
     *  @param arg0 is the Observable object
     *  @param arg1 is empty
     */
    @Override
    public void update(Observable arg0, Object arg1) {
        repaint();
    }
}
